import { BASE_URL } from '../../apis/requests.js';
// pages/getBooksBy/getBooksBy.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    picker: ["书名", "种类", "出版社", "作者"], //与index联系
    index: 0,
    searchName: "",
    books: [],
    isOrder: false
  },

  bindOrder(e) {
    const that = this
    //console.log(e.currentTarget.dataset.index)
    //console.log(this.data.books[e.currentTarget.dataset.index].book_id)
    wx.showModal({
      title: "提示",
      content: "确定预约这本书吗？",
      success(res) {
        //点击确定后的操作
        if (res.confirm) {
          that.setData({
            isOrder: true
          })
        }
      },
      complete() {
        if (that.data.isOrder) {
          const values = {
            "token": wx.getStorageSync('token'),
            "book_id": that.data.books[e.currentTarget.dataset.index].book_id,
          }
          //发送预约请求
          wx.request({
            url: BASE_URL + "/user/order",
            data: values,
            header: {
              'content-type': 'application/json'
            },
            method: 'POST',
            dataType: 'json',
            success: (result) => {
              console.log(result)
              if (result.data.code === "200") {
                wx.showModal({
                  title: "提示",
                  content: "预约成功！",
                  showCancel: false,
                  success(res) {
                    wx.navigateBack({
                      complete: (res) => {},
                    })
                  }
                })
              } else {
                wx.showModal({
                  title: "提示",
                  content: result.data.msg,
                  showCancel: false,
                  success(res) {
                    wx.navigateBack({
                      complete: (res) => {},
                    })
                  }
                })
              } 
                
            },
            fail: () => {},
            complete: () => {}
          })
        }
      }
    })



  },

  PickerChange(e) {
    this.setData({
      index: parseInt(e.detail.value)
    })
  },

  inputChange(e) {
    this.setData({
      searchName: e.detail.value
    })
  },

  search() {
    const values = {
      "token": wx.getStorageSync('token'),
      "type": this.data.index,
       "info": this.data.searchName
    }

    // console.log("回车")
    if (this.data.searchName.length === 0) {
      return
    }
    wx.request({
      url: BASE_URL + "/book/findbooks",
      data: values,
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      dataType: 'json',
      success: (result) => {
        console.log(result)
        if (result.data.code === "200" && result.data.data) {
          this.setData({
            books: result.data.data
          })
        } else {
          wx.showModal({
            title: "提示",
            content: "查询失败，请重试！",
            showCancel: false,
            success(res) {
            }
          })
        }
      
      },
      fail: () => {},
      complete: () => {}
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})